//
//  HJTableCell.m
//  newOcWeibo
//
//  Created by heJing on 15/7/28.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HJTableCell.h"
#import "UIImageView+WebCache.h"
#import "HJPictureCell.h"
#import "HJPhoto.h"

@interface HJTableCell()<UICollectionViewDataSource,UICollectionViewDelegate>
/** 头像*/
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
/** 昵称*/
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
/** 时间*/
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
/** 来源*/
@property (weak, nonatomic) IBOutlet UILabel *sourceLabel;
/** 正文*/
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
/** 会员图标*/
@property (weak, nonatomic) IBOutlet UIImageView *vipView;
// 认证图标
@property (weak, nonatomic) IBOutlet UIImageView *verifiedView;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photosViewHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoViewWidth;

// 底部工具条
@property (weak, nonatomic) IBOutlet UIView *bottomToolView;

@end

@implementation HJTableCell

-(void)awakeFromNib
{
    // 预估高度之后可以有效计算。
    // 设置正文最大的宽度
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}

+ (NSString *)identifier{
    return @"cell";
}

- (void)setStatus:(HJStatus *)status
{
    _status = status;
    
    HJUser *user = _status.user;
    // 头像
    NSURL *url = [NSURL URLWithString:user.profile_image_url];
    [self.iconView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"iOS8.jpg"]];
    // 昵称
    self.nameLable.text = user.name;
    // 会员图标
    UIImage *vipImage = [user vipImage];
    if (vipImage != nil) {
        self.vipView.image = vipImage;
        self.nameLable.textColor = [UIColor orangeColor];
    }else
    {
        // 注意: 重用问题
        self.vipView.image = nil;
        self.nameLable.textColor = [UIColor blackColor];
    }

    // 时间
    self.timeLabel.text = _status.created_at;
    // 来源
    self.sourceLabel.text = _status.source;
    // 正文
    self.contentLabel.text = _status.text;
    // 认证图标
    self.verifiedView.image = [user verifiedImage];


    
    CGSize photosSize = [self caclPhotosSize];
    self.photosViewHeight.constant = photosSize.height;
    self.photoViewWidth.constant = photosSize.width;

}

-(CGSize)caclPhotosSize
{
    // 1.处理没有配图的情况
    NSUInteger count = self.status.pic_urls.count;
    if (count == 0) {
        return CGSizeZero;
    }
    
    // 2.计算行数列数
    NSUInteger maxCols = count == 4 ? 2 : 3;
    //    NSUInteger col = count > 3? 3: count;
    NSUInteger col = count > 3? maxCols : count;
    NSUInteger row = 1;
    if (count % 3 == 0) {
        row = count / 3;
    }else
    {
        row = count / 3 + 1;
    }
    
    // 3.计算宽高
    CGFloat pictureHeigth = 90;
    CGFloat pictureWidth = 90;
    CGFloat pictureMargin = 10; // 间隙
    
    // 宽度 = 列数 * 配图的宽度 + (列数 - 1) * 间隙
    CGFloat photosWidth = col * pictureWidth + (col - 1) * pictureMargin;
    // 高度= 行数 * 配图的高度 + (行数 - 1) * 间隙
    CGFloat photosHeight = row * pictureHeigth + (row - 1) * pictureMargin;
    
    return CGSizeMake(photosWidth, photosHeight);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return  self.status.pic_urls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建图片cell
    HJPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HJPictureCell identifier] forIndexPath:indexPath];
    
    // 2.获取对应行的图片模型
    HJPhoto *photo = self.status.pic_urls[indexPath.item];
    // 3.设置数据
    cell.photo = photo;
    // 4.返回cell
    return cell;

}

#pragma mark - 外部方法
- (CGFloat)cellHeightWithStatus:(HJStatus *)status
{
    // 1.设置数据, 便于系统内部计算控件的尺寸
    self.status = status;
    
    // 2.强制更新布局
    [self layoutIfNeeded];
    
    // 3.返回cell的高度, cell的高度就是底部工具条最大的Y
    return CGRectGetMaxY(self.bottomToolView.frame);
}


@end
